package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.behavior.dtos.ApArticleRelationDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    ApLikesBehaviorService apLikesBehaviorService;
    @Autowired
    ApUnLikesBehaviorService apUnLikesBehaviorService;
    /**
     * 查询文章关系
     * @param dto
     * @return
     */
    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        Map map = new HashMap();
        map.put("islike",false);
        map.put("isunlike",false);
        //参数校验
        if(dto==null||dto.getEntryId()==null||dto.getArticleId()==null){
            return map;
        }
        //查询行为实体对象
        ApBehaviorEntry apBehaviorEntry;
        if(dto.getType()==1){
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(), null);
        }else {
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEntryId());
        }
        if(apBehaviorEntry==null){
            return map;
        }
        //根据行为实体用户id和文章id查询是否点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));
        if(apLikesBehavior!=null && apLikesBehavior.getOperation()==(short)0){
            map.put("islike",true);
        }
        //根据行为实体用户id和文章id查询是否不喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnLikesBehaviorService.getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId()));
        if(apUnlikesBehavior!=null && apUnlikesBehavior.getType()==(short)0){
            map.put("isunlike",true);
        }
        map.put("entryId",apBehaviorEntry.getId());
        return map;

    }
}
